package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import javax.sql.DataSource;

import org.springframework.stereotype.Repository;

import pojo.Customer;
import dao.CustomerDao;

@Repository(value="customerDao")
public class CustomerDaoImpl implements CustomerDao {
	@Resource(name = "cmFactory")
	private EntityManagerFactory factory;
	
	public Customer addCustomer(Customer c) {
		EntityManager manager=null;
		EntityTransaction tx=null;
		
		try {
			manager=factory.createEntityManager();
			tx=manager.getTransaction();
			tx.begin();
			manager.persist(c);
			tx.commit();
		} catch (Exception e) {
			
			e.printStackTrace();
		}finally
		{
			manager.close();
		}
		return c;
	}

	@Override
	public Customer findCustomer(String cname, String password) {
		EntityManager manager=null;
		Customer c=null;
		try {
			manager=factory.createEntityManager();
			String hql="select c from Customer c where c.cname=?1 and c.password=?2";
			Query query=manager.createQuery(hql);
			query.setParameter(1, cname);
			query.setParameter(2, password);
			c=(Customer)query.getSingleResult();
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			manager.close();
		}
		return c;
	}
}





